// 这是在做 AtomGit 讨论时，多个 category ，给予不同背景颜色、hover 颜色

// // 写法一
// function hexToRgba(hex, a = 1) {
//   // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
//   var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
//   const newHex = hex.replace(shorthandRegex, function (m, r, g, b) {
//     return r + r + g + g + b + b;
//   });
//   console.log("newHex", newHex);

//   const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(newHex);

//   const rgbStr = `${parseInt(result[1], 16)},${parseInt(
//     result[2],
//     16
//   )},${parseInt(result[3], 16)}`;

//   return result ? `rgba(${rgbStr},${a})` : null;
// }

function hexToRgba(str, a = 1) {
  const reg = /^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/;
  if (!reg.test(str)) {
    return "rgb(255,255,255)";
  }
  let newStr = str.toLowerCase().replace(/\#/g, "");
  const len = newStr.length;
  if (len === 3) {
    let t = "";
    for (let i = 0; i < len; i++) {
      t += newStr.slice(i, i + 1).concat(newStr.slice(i, i + 1));
    }
    newStr = t;
  }
  const arr = []; // 将字符串分隔，两个两个的分隔
  for (let i = 0; i < 6; i += 2) {
    const s = newStr.slice(i, i + 2);
    arr.push(parseInt(`0x${s}`));
  }
  return `rgba(${arr.join(",")},${a})`;
}

// Usage:
// console.log(hexToRgba("#00ff00"));
// console.log(hexToRgba("#00ff00", 0.2));

function convertColor(arr) {
  const colorDict = [
    "#4BB4C7",
    "#E074AB",
    "#CC7D49",
    "#F0856D",
    "#8d542e",
    "#ff8b00",
    "#ffab00",
    "#0052cc",
    "#505f79",
    "#60a222",
    "#cc4388",
    "#5243aa",
    "#ff8f73",
    "#2684ff",
    "#008da6",
    "#6b778c",
    "#00875a",
    "#de350b",
  ];
  const tempArr = [];

  arr.length > 0 &&
    arr.forEach((item, index) => {
      if (colorDict[index]) {
        tempArr.push({
          ...item,
          color: hexToRgba(colorDict[index]),
          hoverColor: hexToRgba(colorDict[index], 0.2),
        });
      } else {
        tempArr.push({
          ...item,
          color: hexToRgba("#6b778c"),
          hoverColor: hexToRgba("#6b778c", 0.2),
        });
      }
    });
  return tempArr;
}

const testColorArr = Array(20).fill({
  value: "公开讨论",
  label: "公开讨论",
});

// convertColor(testColorArr);

// console.log(testColorArr);

const result = convertColor(testColorArr);

console.table(result);
// console.table(result[18]);
